package com.zsinfo.guoss.supplier.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.ResultsData;
import com.example.netcenter.bean.SupplierAccountBean;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.fragment.PaySetting.Fragment_PwdSettings;
import com.zsinfo.guoss.supplier.fragment.PaySetting.Fragment_Validate;
import com.zsinfo.guoss.supplier.fragment.PaySetting.Fragment_pay_settings;
import com.zsinfo.guoss.supplier.utils.SharedPreferencesUtil;

import java.util.Objects;


public class PaySettingsActivity extends BaseActivity {

    private TextView tv_back;
    private FrameLayout fl_main;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private Fragment mCurrentFragment;
    public int setFlag = 0;
    public String SMSCode = "";


    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_pay_settings;
    }

    @Override
    protected void initView() {

        setTitle("支付设置");

        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        fl_main = (FrameLayout) this.findViewById(R.id.fl_main);


    }

    @Override
    protected void initNetData() {
        Fragment_pay_settings fragmentPaySettings = new Fragment_pay_settings();
        Fragment_Validate fragmentValidate = new Fragment_Validate();
        Fragment_PwdSettings fragmentPwdSettings = new Fragment_PwdSettings();


        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().SUPPLIER_ACCOUNT(ConstantsCode.supplier_account,
                        SharedPreferencesUtil.getID()), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        ResultsData<SupplierAccountBean> resultsData = (ResultsData<SupplierAccountBean>) data;
                        SupplierAccountBean supplierAccountBean = resultsData.getData();
                        /**
                         * 在这里进行添加页面，设置fragment的TAG值
                         */
                        Bundle extras = getIntent().getExtras();
                        //判断bundle是否为空，null 表示没有从上一个界面 传值过来
                        if (extras != null) {
                            if (Objects.equals(extras.getString("fragment_switch"), "Fragment_Validate")) {
                                //表示是从“订单支付”进入的
                                getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment_Validate()).commit();
                            }
                        } else {
                            if (supplierAccountBean.getPassword().isEmpty() || supplierAccountBean.getPassword() == null) {
                                // TODO: 2018/12/5  判断是否是已经设置过密码,设置过就跳过验证码的步骤
                                //表示是从“支付设置”进入的
                                getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment_Validate()).commit();
                            } else {
                                getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, new Fragment_PwdSettings()).commit();
                            }
                        }
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        if (e.getMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            showToast(errorBean.getMsg());

                        } else {
                            showToast(e.getMessage());
                        }
                    }

                    @Override
                    public void complete() {

                    }
                });


    }

    @Override
    protected void customDestroy() {

    }

    /**
     * 主activity进行控制不同的fragment
     *
     * @param toTag
     */
    public void switchFragment(String toTag) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (toTag.equals("Fragment_Validate")) {
            transaction.replace(R.id.fl_main, new Fragment_Validate()).commit();
        } else if (toTag.equals("Fragment_PwdSettings")) {
            transaction.replace(R.id.fl_main, new Fragment_PwdSettings()).commit();
        }
    }

}
